using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace MobileEditor
{
	public partial class MainForm : Form
	{
		private Dictionary<BufferSettings, EditorPage> _bufferEditors;
		private GlobalSettings _globalSettings;


		public MainForm()
		{
			InitializeComponent();
		}


		public Dictionary<BufferSettings, EditorPage> BufferEditors
		{
			get { if (_bufferEditors == null) _bufferEditors = new Dictionary<BufferSettings, EditorPage>(); return _bufferEditors; }
			set { _bufferEditors = value; }
		}

		public GlobalSettings GlobalSettings
		{
			get { if (_globalSettings == null) _globalSettings = new GlobalSettings(); return _globalSettings; }
			set { _globalSettings = value; }
		}

		private BufferSettings SelectedBuffer
		{
			get { return (SelectedEditor != null ? SelectedEditor.BufferSettings : null); }
			set { SelectedEditor = BufferEditors[value]; }
		}

		private EditorPage SelectedEditor
		{
			get
			{
				EditorPage editor = null;
				if (tcEditor.SelectedIndex >= 0)
				{
					editor = tcEditor.TabPages[tcEditor.SelectedIndex] as EditorPage;
				}
				return editor;
			}
			set
			{
				tcEditor.SelectedIndex = tcEditor.TabPages.IndexOf(value);
				miMenuViewWordWrap.Checked = value.WordWrap;
			}
		}


		private void AddBuffer(BufferSettings buffer)
		{
			EditorPage page = new EditorPage(buffer);
			page.GlobalSettings = GlobalSettings;

			tcEditor.TabPages.Add(page);
			SelectedEditor = page;
			cboBuffers.Items.Add(buffer);

			BufferEditors[buffer] = page;
		}

		private void HandleException(Exception ex)
		{
			MessageBox.Show(ex.ToString());
		}


		private void cboBuffers_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				SelectedBuffer = (BufferSettings)cboBuffers.SelectedItem;
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void EditorForm_Load(object sender, EventArgs e)
		{
			try
			{
				GlobalSettings.General.DefaultMask = new FilenameMask("Text Files (*.txt)", "*.txt");
				GlobalSettings.General.Masks.Add(new FilenameMask("All Files (*.*)", "*.*"));
				GlobalSettings.General.Masks.Add(new FilenameMask("Config Files (*.config)", "*.config"));
				GlobalSettings.General.Masks.Add(GlobalSettings.General.DefaultMask);
				GlobalSettings.General.Masks.Add(new FilenameMask("XML Files (*.xml)", "*.xml"));
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void miFileClose_Click(object sender, EventArgs e)
		{
			try
			{
				if (SelectedEditor != null)
				{
					if (SelectedEditor.CloseFile())
					{
						BufferEditors.Remove(SelectedEditor.BufferSettings);
						tcEditor.TabPages.RemoveAt(tcEditor.TabPages.IndexOf(SelectedEditor));
					}
				}
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void miFileExit_Click(object sender, EventArgs e)
		{
			try
			{
				Close();
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void miFileNew_Click(object sender, EventArgs e)
		{
			try
			{
				AddBuffer(new BufferSettings("Untitled-1"));
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void miFileOpen_Click(object sender, EventArgs e)
		{
			try
			{
				FileExplorerForm frm = new FileExplorerForm();
				frm.AddMasks(GlobalSettings.General.Masks);
				frm.EnablePathChange = true;
				frm.FileMustExist = true;
				frm.MultiSelect = false;
				frm.SelectedMask = GlobalSettings.General.DefaultMask;
				frm.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
				frm.Text = "Open File";
				if (frm.ShowDialog() == DialogResult.OK)
				{
					if (frm.SelectedFile.Length > 0)
					{
						string filename = Path.Combine(frm.SelectedPath, frm.SelectedFile);
						AddBuffer(new BufferSettings("", filename));
					}
				}
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void miFileSave_Click(object sender, EventArgs e)
		{
			try
			{
				if (SelectedEditor != null) SelectedEditor.SaveFile();
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void miFileSaveAs_Click(object sender, EventArgs e)
		{
			try
			{
				if (SelectedEditor != null) SelectedEditor.SaveFileAs();
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void miMenuViewWordWrap_Click(object sender, EventArgs e)
		{
			try
			{
				miMenuViewWordWrap.Checked = !miMenuViewWordWrap.Checked;
				if (SelectedEditor != null) SelectedEditor.WordWrap = miMenuViewWordWrap.Checked;
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}
	}
}